package cn.bdqn.biz;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;



public class Myproxy implements MethodInterceptor{
	
	/**
	 * 用Enhancer生成一个原有类的子类，并设置好callback到proxy，
	 * 则原来类的每个方法调用都会转为调用实现了MethodInterceptor接口的proxy的intercept（）函数
	 */
	private Enhancer enhancer = new Enhancer();
	
	public Object getProxy(Class clazz){
		enhancer.setSuperclass(clazz);
		enhancer.setCallback(this);
		return enhancer.create();
	}
	
	
	@Override
	public Object intercept(Object target, Method method,Object[] args, 
				MethodProxy methodProxy) throws Throwable{
		//注入需要调用的方法
		command();
		Object obj = methodProxy.invokeSuper(target, args);
		//注入需要调用的方法
		award();
		return obj;
	}
	
	private void command(){
		System.out.println("驯兽师发出命令！");
	}
	
	private void award(){
		System.out.println("驯兽师给予奖励！");
	}

	
	
}
